import * as callServices from '../services/service'

export default {
  namespace: 'table',
  // 状态
  state: {
    treeData: [],
    tableData: [],
    selectedKeys: [],
    pageState: {
      page: 1,
      pageSize: 10,
      total: null,
    },
  },
  // reducers对象： 把要更新的数据给进去并返回 就可以更新视图了
  reducers: {
    // 更新 表格相关
    updateTable (state, { payload: { tableData, selectedKeys, pageState } }){
      return { ...state, tableData, selectedKeys, pageState }
    },
    // 更新 树相关
    updateTree (state, { payload: { treeData } }){
      return { ...state, treeData }
    },
  },
  // effects对象： 异步操作（包括请求、定时器等）放这里
  effects: {
    *queryTree ({ payload: { pageParams } }, { put, call }){
      // 1.请求tree数据并赋值给treeData
      let treeData = yield call(callServices.queryTree)
      treeData = [treeData]
      // 2.更新树
      yield put({ type: 'updateTree', payload: { treeData }})
      // 3.获取表格数据
      yield put({ type: 'queryTable', payload: { pageParams }})
    },
    *onTreeSelect ({ payload: selectedKeys }, { put }){
      // console.log(selectedKeys)
      yield put({ type: 'queryTable', payload: { selectedKeys } })
    },
    *queryTable ({ payload: { filterParams = {}, pageParams = {}, selectedKeys = [] } }, { put, call }) {
      // 1. 分页默认值 处理入参
      const { page = 1, pageSize = 10 } = pageParams
      // console.log( filterParams, pageParams, selectedKeys )
      if( selectedKeys[0] ) filterParams = { ...filterParams, "deptId": selectedKeys[0] }
      const pageIn = { topagenum: page, rowsperpage: pageSize }  //请求传入的参数对象
      // 2.请求表格数据并赋值
      const { userInfo, pageOut } = yield call(callServices.queryTable, { filterParams, pageIn })
      const pageState = { page, pageSize, total: pageOut.rowcountall } //state中管理的分页对象
      // 3.更新表格
      yield put({ type: 'updateTable', payload: { tableData: userInfo, selectedKeys, pageState }})
    },
    
    *filter ({ payload }, { put }){
      // console.log(payload)
      yield put({ type: 'queryTable', payload: { filterParams: payload }})
    },
    *delete ({ payload: userId }, { put, call }){
      // console.log(userId)
      yield call(callServices.del, {userId})
      yield put({ type: 'reload' })
    },
    *edit ({ payload: { userId, record } }, { put, call }){
      // console.log( userId, record )
      let params = { ...record, userId }
      yield call(callServices.edit, params)
      yield put({ type: 'reload' })
    },
    *add ({ payload: record }, { put, call }){
      // console.log(record)
      yield call(callServices.add, record)
      yield put({ type: 'reload' })
    },
    // 重载
    *reload (action, { put, select }){
      // 用select获取state
      const { page, pageSize } = yield select(_ => _.table.pageState)
      yield put({ type: 'queryTable', payload: { pageParams: { page, pageSize } } })
    },
  },
  //subscriptions对象： 监听事件放这里，比如监听键盘、路由
  subscriptions: {
    setup ({ dispatch, history }){
      return history.listen(({ pathname, query }) => {
        if (pathname === '/demo') {
          const { page, pageSize } = query
          const pageParams = { page, pageSize }
          dispatch({ type: 'queryTree', payload: { pageParams }})
        }
      })
    },
  },
}
